//
//  ViewController.m
//  动力学元素UIDynamic
//
//  Created by 伍银荣 on 2021/7/14.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"


@interface BGView : UIView
@property (nonatomic, assign) CGRect redRect;

@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGPoint endPoint;
@end

@implementation BGView

-(void)drawRect:(CGRect)rect {
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(0, 200)];
//    [path addLineToPoint:CGPointMake(200, 250)];
//    [path stroke];
  //  [[UIBezierPath bezierPathWithRect:_redRect] stroke];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:self.startPoint];
    [path addLineToPoint:self.endPoint];
    [path stroke];
}

@end

@interface ViewController ()<UICollisionBehaviorDelegate>  //碰撞的代理
@property (nonatomic, weak) UIView *redView;
@property (nonatomic, weak) UIView *blueView;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, strong) UIAttachmentBehavior *attachment;

@end

@implementation ViewController

//loadView的优先级大于从storyBoard中加载的view
-(void)loadView {
    //手动创建bgView,没有和storyBoard关联
    self.view = [[BGView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [UIColor whiteColor];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    redView.alpha = 0.7;
    [self.view addSubview:redView];
    self.redView = redView;
    
   // UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(230, self.view.frame.size.height - 150, 50, 50)];
    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(230, 100, 100, 100)];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    self.blueView = blueView;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self attachmentWithTouches:touches];
   // [self snapWithTouches:touches];
}

//动力学元素: (吸附行为)甩行为
- (void)snapWithTouches:(NSSet <UITouch *> *)touches {
    //创建触摸对象
    UITouch *t = touches.anyObject;
    //获取当前手指的位置
    CGPoint currentPoint = [t locationInView:t.view];
    //创建动画者对象
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    //创建甩行为
    UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:self.redView snapToPoint:currentPoint];
    //设置阻尼,阻尼越大,甩出去的行为越慢
    snap.damping = 10;
    //添加行为
    [self.animator addBehavior:snap];
    
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //创建触摸对象
    UITouch *t = touches.anyObject;
    //获取当前手指的位置
    CGPoint currentPoint = [t locationInView:t.view];
    self.attachment.anchorPoint = currentPoint;
    __weak ViewController * weakSelf = self;
    self.attachment.action = ^{
        BGView *bgView = (BGView *)weakSelf.view;
        bgView.startPoint = weakSelf.redView.center;
        bgView.endPoint = currentPoint;
        [weakSelf.view setNeedsDisplay];
    };
    
}

//动力学元素: 附着行为(甩行为)
- (void)attachmentWithTouches:(NSSet <UITouch *> *)touches {
   
    //创建触摸对象
    UITouch *t = touches.anyObject;
    //获取当前手指的位置
    CGPoint currentPoint = [t locationInView:t.view];
    //创建动画者对象
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    //创建附着行为
   // self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.redView attachedToAnchor:currentPoint];
    //设置item和item之间的附着行为
    //self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.redView attachedToItem:self.blueView];
    self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.redView offsetFromCenter:UIOffsetMake(20, 20) attachedToAnchor:currentPoint];
    //设置附着行为的阻尼,临界阻尼为1
    self.attachment.damping = 0.5;
    //频率: 赫兹
    self.attachment.frequency = 0.5;  //如果想要变成弹性附着,就需要设置这个属性
    self.attachment.length = 150;
    //添加行为
    [self.animator addBehavior:self.attachment];
    
    //重力行为
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[self.redView]];
    [self.animator addBehavior:gravity];
}


//动力学元素: 重力
- (void)gravity {
    //重力
    //创建动画者对象
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    //创建重力行为
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[self.redView]];
    //设置重力行为的方向
    //1.以设置角度的方式设置方向
    //gravity.angle = M_PI;
    //2.设置向量值来设置方向
    gravity.gravityDirection = CGVectorMake(200, 200);
    //量级,量级越大,往下掉的越快
    gravity.magnitude = 1;
    //将重力行为添加到动画者对象中
    [self.animator addBehavior:gravity];
}


//动力学元素: 碰撞
- (void)collision {
    //根据某一个范围,创建动画者对象
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    //重力
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[self.redView]];
    //根据某一个动力学元素,创建行为
    //碰撞  与另一个item(blueView)发生碰撞
    UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[self.redView, self.blueView]];
    
    //把引用的view的ebounds变成边界
    collision.translatesReferenceBoundsIntoBoundary = YES;
    
    //设置碰撞的模式
//    UICollisionBehaviorModeItems        仅仅item和item发生碰撞
//    UICollisionBehaviorModeBoundaries   仅仅是item和边界发生碰撞
//    UICollisionBehaviorModeEverything   item和边界都发生碰撞(默认)
    collision.collisionMode = UICollisionBehaviorModeEverything;
    
    //添加边界
    //1.以一条线为边界(从一个点到另一个点),Identifier是不能为空的
    [collision addBoundaryWithIdentifier:@"key" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(200, 250)];
    
    //以路径的方式添加边界(将blueView的frame设置为路径)
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];
    [collision addBoundaryWithIdentifier:@"key2" forPath:path];
    
    
    
//    //action实时监听,系统会在view动的时候执行这个block
//    collision.action = ^{
//        //监听redView的frame的变化
//        NSLog(@"%@",NSStringFromCGRect(self.redView.frame));
//        //查看绘制的frame大小
//        BGView *bgView = (BGView *)self.view;
//        bgView.redRect = self.redView.frame;
//        //重绘
//        [self.view setNeedsDisplay];
//        //如果redView的宽大于100,改变颜色
//        if (self.redView.frame.size.width > 100) {
//            self.redView.backgroundColor = [UIColor yellowColor];
//        } else {
//            self.redView.backgroundColor = [UIColor redColor];
//        }
//    };
    //设置代理
    collision.collisionDelegate = self;
    //将行为添加到动画者中
    [self.animator addBehavior:collision];
    //添加重力行为
    [self.animator addBehavior:gravity];
}


#pragma mark - 碰撞的四个代理方法
//item和item开始碰撞
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 atPoint:(CGPoint)p {
    
}

//item和item结束碰撞
-(void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 {
    
}

//item和边界开始发生碰撞
-(void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p {
    //判断碰撞的边界是哪一个
    NSString *str = (NSString *)identifier;
    if ([str isEqualToString:@"key"]) {
        self.redView.backgroundColor = [UIColor yellowColor];
    } else if ([str isEqualToString:@"key2"]) {
        self.redView.backgroundColor = [UIColor orangeColor];
    } else {
        self.redView.backgroundColor = [UIColor redColor];
    }
}

//item和边界结束碰撞
-(void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier {
    
}


@end
